﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class Sponsor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Sponsor_PageTitle;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            String SponsorId = null;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            if (ou.UserAccessLevel != ("5"))
            {
                Response.Redirect("~/MainNewsPage.aspx");
            }
            
            if (Request.QueryString["SponsorId"] != null)
            {
                SponsorId = Request.QueryString["SponsorId"].ToString();
                this.SqlDataSource1.SelectParameters.Add("SponsorId", SponsorId);
            }
            if (SponsorId == null)
                Response.Redirect("GeneralMessage.aspx?MessageString=l43jOTHBB");

            if (!IsPostBack)
            {
                this.SponsorNameTextBox.Text = (GND.ReadNivdalStringData("@SponsorId", "Nivdal_GetSponsorName", SponsorId));
                this.ImageFileNameTextBox.Text = (GND.ReadNivdalStringData("@SponsorId", "Nivdal_GetSponsorImageFileName", SponsorId));
                this.LinkTextBox.Text = (GND.ReadNivdalStringData("@SponsorId", "Nivdal_GetSponsorLink", SponsorId));
                this.PriorityDropDownList.SelectedValue = (GND.ReadNivdalIntData("@SponsorId", "Nivdal_GetSponsorPriority", SponsorId));
                this.SponsorContactsLimitTextBox.Text = (GND.ReadNivdalIntData("@SponsorId", "Nivdal_GetSponsorContactsLimit", SponsorId));

            }

            
        }

        protected void ChangeNameButton_Click(object sender, EventArgs e)
        {
            String SponsorName = this.SponsorNameTextBox.Text.Trim();
            String SponsorId = null;

            if (Request.QueryString["SponsorId"] != null)
                SponsorId = Request.QueryString["SponsorId"].ToString();
            if (SponsorId == null)
                Response.Redirect("GeneralMessage.aspx?MessageString=l43jOTHBB");

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_UpdateSponsorName";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_SponsorId = new SqlParameter();
            dbParam_SponsorId.ParameterName = "@SponsorId";
            dbParam_SponsorId.Value = SponsorId;
            dbParam_SponsorId.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_SponsorId);

            SqlParameter dbParam_Name = new SqlParameter();
            dbParam_Name.ParameterName = "@Name";
            dbParam_Name.Value = SponsorName;
            dbParam_Name.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Name);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                this.FeedbackMessageLabel.Text = dbParam_Answer.Value.ToString();
            }
            catch (Exception SQLDBException)
            {
                this.FeedbackMessageLabel.Text = Convert.ToString(SQLDBException);
                return;

            }

            finally
            {
                dbConnection.Close();
            }

        }


        protected void ChangeImageFileNameButton_Click(object sender, EventArgs e)
        {
            String ImageFileName = this.ImageFileNameTextBox.Text.Trim();
            String SponsorId = null;

            if (Request.QueryString["SponsorId"] != null)
                SponsorId = Request.QueryString["SponsorId"].ToString();
            if (SponsorId == null)
                Response.Redirect("GeneralMessage.aspx?MessageString=l43jOTHBB");

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_UpdateSponsorImageFile";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_SponsorId = new SqlParameter();
            dbParam_SponsorId.ParameterName = "@SponsorId";
            dbParam_SponsorId.Value = SponsorId;
            dbParam_SponsorId.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_SponsorId);

            SqlParameter dbParam_Name = new SqlParameter();
            dbParam_Name.ParameterName = "@ImageFileName";
            dbParam_Name.Value = ImageFileName;
            dbParam_Name.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Name);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                this.FeedbackMessageLabel.Text = dbParam_Answer.Value.ToString();
            }
            catch (Exception SQLDBException)
            {
                this.FeedbackMessageLabel.Text = Convert.ToString(SQLDBException);
                return;

            }

            finally
            {
                dbConnection.Close();
            }

        }


        protected void ChangeLinkButton_Click(object sender, EventArgs e)
        {
            String Link = this.LinkTextBox.Text.Trim();
            String SponsorId = null;

            if (Request.QueryString["SponsorId"] != null)
                SponsorId = Request.QueryString["SponsorId"].ToString();
            if (SponsorId == null)
                Response.Redirect("GeneralMessage.aspx?MessageString=l43jOTHBB");

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_UpdateSponsorLink";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_SponsorId = new SqlParameter();
            dbParam_SponsorId.ParameterName = "@SponsorId";
            dbParam_SponsorId.Value = SponsorId;
            dbParam_SponsorId.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_SponsorId);

            SqlParameter dbParam_Name = new SqlParameter();
            dbParam_Name.ParameterName = "@Link";
            dbParam_Name.Value = Link;
            dbParam_Name.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Name);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                this.FeedbackMessageLabel.Text = dbParam_Answer.Value.ToString();
            }
            catch (Exception SQLDBException)
            {
                this.FeedbackMessageLabel.Text = Convert.ToString(SQLDBException);
                return;

            }

            finally
            {
                dbConnection.Close();
            }

        }

        protected void ChangeContractsLimitButton_Click(object sender, EventArgs e)
        {
            String ContractsLimit = this.SponsorContactsLimitTextBox.Text.Trim();
            String SponsorId = null;

            if (Request.QueryString["SponsorId"] != null)
                SponsorId = Request.QueryString["SponsorId"].ToString();
            if (SponsorId == null)
                Response.Redirect("GeneralMessage.aspx?MessageString=l43jOTHBB");

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_UpdateSponsorContractsLimit ";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_SponsorId = new SqlParameter();
            dbParam_SponsorId.ParameterName = "@SponsorId";
            dbParam_SponsorId.Value = SponsorId;
            dbParam_SponsorId.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_SponsorId);

            SqlParameter dbParam_Name = new SqlParameter();
            dbParam_Name.ParameterName = "@ContractsLimit";
            dbParam_Name.Value = ContractsLimit;
            dbParam_Name.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Name);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                this.FeedbackMessageLabel.Text = dbParam_Answer.Value.ToString();
            }
            catch (Exception SQLDBException)
            {
                this.FeedbackMessageLabel.Text = Convert.ToString(SQLDBException);
                return;

            }

            finally
            {
                dbConnection.Close();
            }

        }



        protected void ChangepriorityButton_Click(object sender, EventArgs e)
        {
            String Priority = this.PriorityDropDownList.SelectedValue;
            String SponsorId = null;

            if (Request.QueryString["SponsorId"] != null)
                SponsorId = Request.QueryString["SponsorId"].ToString();
            if (SponsorId == null)
                Response.Redirect("GeneralMessage.aspx?MessageString=l43jOTHBB");

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_UpdateSponsorPriority";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_SponsorId = new SqlParameter();
            dbParam_SponsorId.ParameterName = "@SponsorId";
            dbParam_SponsorId.Value = SponsorId;
            dbParam_SponsorId.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_SponsorId);

            SqlParameter dbParam_Name = new SqlParameter();
            dbParam_Name.ParameterName = "@Priority";
            dbParam_Name.Value = Priority;
            dbParam_Name.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_Name);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                this.FeedbackMessageLabel.Text = dbParam_Answer.Value.ToString();
            }
            catch (Exception SQLDBException)
            {
                this.FeedbackMessageLabel.Text = Convert.ToString(SQLDBException);
                return;

            }

            finally
            {
                dbConnection.Close();
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}